package com.geeguo.ebuilder.operation.controller.tenant.tenantinfo;

import com.geeguo.ebuilder.core.common.action.ActionResult;
import com.geeguo.ebuilder.core.common.model.vo.PageListVO;
import com.geeguo.ebuilder.core.common.utils.key.UUIDCheckUtils;
import com.geeguo.ebuilder.core.common.utils.key.UUIDGenerator;
import com.geeguo.ebuilder.core.security.annotation.RequiresPermissions;
import com.geeguo.ebuilder.operation.base.core.constants.MessageConstants;
import com.geeguo.ebuilder.operation.base.core.dictionary.ClientTypeEnum;
import com.geeguo.ebuilder.operation.base.core.dictionary.LogTypeEnum;
import com.geeguo.ebuilder.operation.base.log.annotation.Log;
import com.geeguo.ebuilder.operation.base.security.model.LoginUser;
import com.geeguo.ebuilder.operation.base.security.utils.AuthUtils;
import com.geeguo.ebuilder.operation.business.tenant.datasourceinfo.model.DataSourceInfoVO;
import com.geeguo.ebuilder.operation.business.tenant.datasourceinfo.service.DataSourceInfoService;
import com.geeguo.ebuilder.operation.business.tenant.tenantinfo.model.*;
import com.geeguo.ebuilder.operation.business.tenant.tenantinfo.service.TenantInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Tag(name = "租户信息")
@RestController
public class TenantInfoController {
    
    @Autowired
    private TenantInfoService tenantInfoService;
    @Autowired
    private DataSourceInfoService dataSourceInfoService;
    
    @Operation(summary = "分页查询租户信息")
    @PostMapping("/tenant/tenantInfo/list")
    @RequiresPermissions("Tenant:TenantInfo")
    public ActionResult<PageListVO<TenantInfoVO>> list(@RequestBody TenantInfoQuery query) {
        return ActionResult.page(tenantInfoService.list(query));
    }
    
    @Operation(summary = "保存租户信息")
    @PostMapping("/tenant/tenantInfo/save")
    @RequiresPermissions("Tenant:TenantInfo")
    public ActionResult<Boolean> save(@Validated @RequestBody TenantInfoCreateDTO dto) {
        LoginUser currentUser = AuthUtils.getLoginUser();
        TenantInfoEntity tenantInfo = TenantInfoConverter.INSTANCE.convert2Entity(dto);
        tenantInfo.setId(UUIDGenerator.getId());
        tenantInfo.setCreateTime(new Date());
        tenantInfo.setCreateUser(currentUser.getUserId());
        if (tenantInfoService.save(tenantInfo)) {
            return ActionResult.success();
        }
        return ActionResult.fail();
    }
    
    @Operation(summary = "根据ID获取租户信息")
    @GetMapping("/tenant/tenantInfo/get/{id}")
    @RequiresPermissions("Tenant:TenantInfo")
    public ActionResult<TenantInfoVO> get(@PathVariable("id") String id) {
        if (UUIDCheckUtils.check(id)) {
            TenantInfoVO tenantInfo = tenantInfoService.get(id);
            if (tenantInfo != null) {
                return ActionResult.success(tenantInfo);
            }
            return ActionResult.fail(MessageConstants.FAIL_DATA_NOT_EXIST);
        }
        return ActionResult.fail(MessageConstants.FAIL_REQUEST_DATA_ERROR);
    }

    @Operation(summary = "更新租户信息")
    @PostMapping("/tenant/tenantInfo/update")
    @RequiresPermissions("Tenant:TenantInfo")
    public ActionResult<Boolean> update(@Validated @RequestBody TenantInfoUpdateDTO dto) {
        LoginUser currentUser = AuthUtils.getLoginUser();
        TenantInfoVO old = tenantInfoService.get(dto.getId());
        if (old != null) {
            TenantInfoEntity tenantInfo = TenantInfoConverter.INSTANCE.convert2Entity(dto);
            tenantInfo.setModifyTime(new Date());
            tenantInfo.setModifyUser(currentUser.getUserId());
            if (tenantInfoService.update(tenantInfo, TenantInfoConverter.INSTANCE.convert2Entity(old))) {
                return ActionResult.success();
            }
        }
        return ActionResult.fail();
    }
    
    @Operation(summary = "删除租户信息")
    @PostMapping("/tenant/tenantInfo/remove")
    @RequiresPermissions("Tenant:TenantInfo")
    @Log(title = "删除租户信息", logType = LogTypeEnum.DELETE, clientType = ClientTypeEnum.Web)
    public ActionResult<Boolean> remove(@RequestParam("id") String id) {
        if (UUIDCheckUtils.check(id)) {
            if (tenantInfoService.remove(id)) {
                return ActionResult.success();
            }
        }
        return ActionResult.fail();
    }

    @Operation(summary = "查询所有数据源信息")
    @GetMapping("/tenant/tenantInfo/listAllDataSourceInfo")
    @RequiresPermissions("Tenant:TenantInfo")
    public ActionResult<List<DataSourceInfoVO>> listAllDataSourceInfo() {
        return ActionResult.success(dataSourceInfoService.listAll());
    }
}
